package com.cci.kangdao.service.impl;


import com.cci.kangdao.aop.UserThreadLocal;
import com.cci.kangdao.dao.BusinessRegionTDao;
import com.cci.kangdao.dao.FaultCodeTDao;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.service.BusinessService;
import com.cci.kangdao.service.FaultCodeService;
import com.cci.kangdao.service.PropertiesUtils;
import com.cci.kangdao.utilTool.ParameterTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("FaultCodeService")
public class FaultCodeServiceImpl implements FaultCodeService {

	private Logger log = Logger.getLogger(this.getClass().getName());
	@Resource
	private FaultCodeTDao faultCodeTDao;
	@Resource
	private PropertiesUtils propertiesUtils;

	//获取故障码集合
	public ModelAndView getFaultCodeList(Map<String, Object> map) {
		HttpServletResponse response = (HttpServletResponse) map.get("response");
		JSONObject obj = new JSONObject();  //声明返给前端json对象
		String keyWord = MapUtils.getString(map,"KeyWord","");
		int page = MapUtils.getInteger(map,"PageNo",1);
		int pageSize = MapUtils.getInteger(map,"PageSize",Integer.valueOf(this.propertiesUtils.getPropertiesValue("pagesize")));
		try {
			PageHelper.startPage(page,pageSize);
			List<Map<String,Object>> faultCodeList = faultCodeTDao.getFaultCodeListByFaultCode(keyWord);
			PageInfo pageInfo = new PageInfo(faultCodeList);
			Map<String,Object> faultCodeListMap = new HashMap<>();
			faultCodeListMap.put("FaultCodeList",pageInfo.getList());
			obj.put("data", faultCodeListMap);
			obj.put("status", 0);
			obj.put("msg", "success");
			ParameterTool.writeResponse(response, obj.toString());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			ParameterTool.writeErrorResponse(response);
			// 主动回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return null;
	}

	


}
